package com.example.yuejian.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.OrientationHelper;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.blankj.utilcode.util.ActivityUtils;
import com.blankj.utilcode.util.SPUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.bumptech.glide.Glide;
import com.bumptech.glide.TransitionOptions;
import com.example.yuejian.R;
import com.example.yuejian.adapter.MasterDetailAdapter;
import com.example.yuejian.adapter.MasterQAAdapter;
import com.example.yuejian.bean.MasterClassEntity;
import com.example.yuejian.bean.MasterEntity;
import com.example.yuejian.bean.MasterQAEntity;
import com.example.yuejian.bean.TWEntity;
import com.example.yuejian.common.Url;
import com.example.yuejian.view.TitleView;
import com.zhouyou.http.EasyHttp;
import com.zhouyou.http.callback.SimpleCallBack;
import com.zhouyou.http.exception.ApiException;

import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

public class MasterActivityDetail extends BaseActivity {


    private static final String TAG = "MasterActivityDetail";
    @BindView(R.id.title_activity)
    TitleView title_activity;
    @BindView(R.id.recy1)
    RecyclerView recycler1;
    @BindView(R.id.recy2)
    RecyclerView recycler2;
    @BindView(R.id.tw_bt)
    Button tw_bt;
    @BindView(R.id.tw_et)
    EditText tw_et;
    @BindView(R.id.img)
    ImageView img;
    @BindView(R.id.clubname)
    TextView clubname;
    @BindView(R.id.name)
    TextView name;
    LinearLayoutManager layoutManager1;
    LinearLayoutManager layoutManager2;
    RecyclerView.Adapter adapter;
    private Intent intent;
    private String mid;
    private Context context;
    private Handler mhandler = new mHandler();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        setContentView(R.layout.activity_master_detail);
        super.onCreate(savedInstanceState);
        context = getBaseContext();
        initView();
        initData();
    }
Integer isCollect;
    String uid;
    private void initData() {

        intent = getIntent();

        mid = intent.getIntExtra("mid", 0) + "";
        uid = intent.getIntExtra("id",1)+"";



        isCollect = SPUtils.getInstance().getInt("master_"+uid,-1);

        if (isCollect == -1) {
            title_activity.getRightTextTv().setText("已收藏");
        }else {
            title_activity.getRightTextTv().setText("收藏");
        }
        //获取标题栏右侧的textview ， 设置点击事件
        title_activity.getRightTextTv().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //修改文本
                if (title_activity.getRightTextTv().getText().toString().equals("已收藏")) {
                    ToastUtils.showShort("取消收藏");
                    title_activity.getRightTextTv().setText("收藏");

                    SPUtils.getInstance().put("master_" + uid, Integer.parseInt(uid));
                }else {
                    ToastUtils.showShort("收藏成功");
                    title_activity.getRightTextTv().setText("已收藏");

                    SPUtils.getInstance().put("master_"+uid,-1);
                }
            }
        });


        EasyHttp.get("yuejian6/public/index.php/json/master/details")
                .params("mid", mid)
                .execute(new SimpleCallBack<String>() {
                    @Override
                    public void onSuccess(String data) {

                        Message msg = Message.obtain(); // 实例化消息对象
                        msg.what = 0; // 消息标识
                        msg.obj = data; // 消息内容存放

                        mhandler.sendMessage(msg);

                    }

                    @Override
                    public void onError(ApiException error) {
                        Log.d(TAG, "onFailed: " + error);
                    }
                });

        EasyHttp.get("yuejian6/public/index.php/json/master/class_list")
                .params("mid", mid)
                .execute(new SimpleCallBack<String>() {
                    @Override
                    public void onSuccess(String data) {

                        Message msg = Message.obtain(); // 实例化消息对象
                        msg.what = 1; // 消息标识
                        msg.obj = data; // 消息内容存放

                        mhandler.sendMessage(msg);

                    }

                    @Override
                    public void onError(ApiException error) {
                        Log.d(TAG, "onFailed: " + error);
                    }
                });
        EasyHttp.get("yuejian6/public/index.php/json/master/question")
                .params("mid", mid)
                .execute(new SimpleCallBack<String>() {
                    @Override
                    public void onSuccess(String data) {

                        Message msg = Message.obtain(); // 实例化消息对象
                        msg.what = 2; // 消息标识
                        msg.obj = data; // 消息内容存放

                        mhandler.sendMessage(msg);

                    }

                    @Override
                    public void onError(ApiException error) {
                        Log.d(TAG, "onFailed: " + error);
                    }
                });

    }

    private void initView() {
        layoutManager1 = new LinearLayoutManager(context);
        layoutManager2 = new LinearLayoutManager(context);
        title_activity.getLeftBackTextTv().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ActivityUtils.finishActivity(MasterActivityDetail.class);
            }
        });
    }

    @OnClick(R.id.tw_bt)
    void tw() {
//        mid=1&user_id=1&intro=1+2=？

        String uid = SPUtils.getInstance().getInt("uid", 0) + "";
        EasyHttp.get("yuejian6/public/index.php/json/master/fbquestion?")
                .params("mid", mid)
                .params("user_id", uid)
                .params("intro", tw_et.getText().toString())
                .execute(new SimpleCallBack<String>() {
                    @Override
                    public void onSuccess(String data) {

                        Message msg = Message.obtain(); // 实例化消息对象
                        msg.what = 3; // 消息标识
                        msg.obj = data; // 消息内容存放

                        mhandler.sendMessage(msg);

                    }

                    @Override
                    public void onError(ApiException error) {
                        Log.d(TAG, "onFailed: " + error);
                    }
                });
    }

    class mHandler extends Handler {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);

            switch (msg.what) {
                case 0:

                    MasterEntity masterEntity = null;
                    try {

                        masterEntity = JSON.parseObject(msg.obj.toString(), MasterEntity.class);
                    } catch (Exception e) {
                        ToastUtils.showShort("数据获取失败");
                    }

                    name.setText(masterEntity.getName());

                    clubname.setText(masterEntity.getClubname());

                    Glide.with(context).load(Url.imgUrl + masterEntity.getPic()).into(img);
                    break;
                case 1:
                    List<MasterClassEntity> masterEntities = null;

                    try {
                        masterEntities = JSON.parseArray(msg.obj.toString(), MasterClassEntity.class);
                    } catch (Exception e) {
                        Log.d(TAG, "handleMessage: " + e.toString());
                        break;
                    }
                    if (masterEntities == null) {
                        return;
                    }


                    adapter = new MasterDetailAdapter(context, masterEntities, R.layout.master_item);

                    //设置布局管理器
                    recycler1.setLayoutManager(layoutManager1);
//设置为垂直布局，这也是默认的
                    layoutManager1.setOrientation(OrientationHelper.VERTICAL);
//设置Adapter
                    recycler1.setAdapter(adapter);
//设置增加或删除条目的动画
                    recycler1.addItemDecoration(new DividerItemDecoration(context, DividerItemDecoration.VERTICAL));
                    recycler1.setItemAnimator(new DefaultItemAnimator());
                    break;

                case 2:

                    List<MasterQAEntity> masterQAEntities = null;

                    try {
                        masterQAEntities = JSON.parseArray(msg.obj.toString(), MasterQAEntity.class);

                    } catch (Exception e) {
                        Log.d(TAG, "handleMessage: " + e.toString());
                    }


                    if (masterQAEntities == null) {
                        return;
                    }
                    adapter = new MasterQAAdapter(context, masterQAEntities, R.layout.item_qa);

                    //设置布局管理器
                    recycler2.setLayoutManager(layoutManager2);
//设置为垂直布局，这也是默认的
                    layoutManager2.setOrientation(OrientationHelper.VERTICAL);
//设置Adapter
                    recycler2.setAdapter(adapter);
//设置增加或删除条目的动画
                    recycler2.addItemDecoration(new DividerItemDecoration(context, DividerItemDecoration.VERTICAL));
                    recycler2.setItemAnimator(new DefaultItemAnimator());

                    break;

                case 3:

                    TWEntity twEntity = null ;
                    try {
                        twEntity = JSON.parseObject(msg.obj.toString(), TWEntity.class);
                    }catch (Exception e){
                        ToastUtils.showShort("获取数据失败");
                        return;
                    }
                    if (twEntity.getError() == 0) {
                        ToastUtils.showShort(twEntity.getMsg());
                    }
                    break;
            }

        }
    }

}
